package com.stopboot.code.generator.utils;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName TypeUtil
 * @Description:
 * @Author: Lianyutao
 * @Date: 2021/6/9 6:05 下午
 * @Version: 1.0.1
 */
@Slf4j
public class TypeUtil {


    public static String dbTypeToJavaType(String dbCloumType) {
        return dbTypeToJavaType(dbCloumType, null);
    }

    /**
     * JDBC 类型	Java Object 类型
     * bigint	Long
     * real	Float
     * date	java.sql.Date
     * time	java.sql.Time
     * timestamp	java.sql.Timestamp
     * <p>
     * 数据库类型转java类型
     *
     * @return
     */
    public static String dbTypeToJavaType(String dbCloumType, Integer dataScale) {

        dbCloumType = dbCloumType.toLowerCase();

        if (dbCloumType.equals("binary") || dbCloumType.equals("varbinary") || dbCloumType.equals("longvarbinary")) {
            return "byte[]";
        }
        if (dbCloumType.equals("bit")) {
            return "Boolean";
        }
        if (dbCloumType.equals("tinyint") || dbCloumType.equals("smallint") || dbCloumType.equals("int") || dbCloumType.equals("integer")) {
            return "Integer";
        }
        if (dbCloumType.equals("real")) {
            return "Float";
        }
        if (dbCloumType.equals("bigint")) {
            return "Long";
        }
        if (dbCloumType.equals("json")) {
            return "String";
        }
        if (dbCloumType.equals("clob") || dbCloumType.equals("varchar2") || dbCloumType.equals("varchar") || dbCloumType.equals("longvarchar") || dbCloumType.equals("text") || dbCloumType.equals("longtext") || dbCloumType.equals("char")) {
            return "String";
        }
        if (dbCloumType.equals("datetime") || dbCloumType.equals("date") || dbCloumType.equals("timestamp") || dbCloumType.equals("timestamp(6)")) {
            return "Date";
        }
        if (dbCloumType.equals("float") || dbCloumType.equals("double")) {
            return "Double";
        }
        if (dbCloumType.equals("number") || dbCloumType.equals("decimal")) {
            if (dataScale != null && dataScale > 0) {
                return "BigDecimal";
            }
            return "BigDecimal";
        }
        return "Object";
    }

    /**
     * 设置列常用默认值
     * @param  dataType
     * @return
     */
    public static Object setColumnDefaultValue(String dataType) {
        if (dataType.equals("int")) {
            return 1;
        }
        if (dataType.equals("varchar")) {
            return "string";
        }
        if (dataType.equals("timestamp")) {
            return "2021-01-01 00:00:00";
        }
        if (dataType.equals("datetime")) {
            return "2021-01-01 00:00:00";
        }
        return "string";
    }
}
